package com.amir.common.api

import android.os.Handler
import com.alibaba.fastjson.JSON
import com.amir.common.beans.BaseResult
import com.amir.common.mvp.view.IView
import com.google.gson.JsonElement
import io.reactivex.Observer
import io.reactivex.disposables.Disposable


abstract class CustomObserver<T>(view: IView?) : Observer<T> {

    private var mIView: IView? = null

    init {
        // OkHttpInterce.setOnErrorCalBackListener(this)
        mIView = view
    }

    override fun onSubscribe(d: Disposable) {
        mIView?.showLoading()
    }

    override fun onNext(t: T) {
        if (t is JsonElement) {
            val baseResult = JSON.parseObject(t.toString(), BaseResult::class.java)
            val code = baseResult.msgCode
            if (code == -1) {
                //统一显示错误提示
            } else if (code == 0 || code == -2) {
                success(t)
            } else if (baseResult.errMsg == "权限受限" || baseResult.errMsg == "access_token已过期") {
                mIView?.onAgainLogin()
            } else if (code == 1004) {
                mIView?.onAgainLogin()
            } else {
                mIView?.onError((t as BaseResult).errMsg)
            }
        } else {
            var result = t as BaseResult
            val code = result.msgCode
            if (code == 0 || code == -2) {
                success(t)
            } else if (code < 0) {
                mIView?.showDialog(result.errMsg, code)

            } else {
                mIView?.onError(result.errMsg)
            }

        }

    }

    /**
     * 回调给页面
     */
    protected abstract fun success(t: T)

    /**
     * 接口请求出错
     */
    override fun onError(e: Throwable) {
        mIView?.onError(e.message)
        mIView?.dismissLoading()
    }

//    override fun onResult(code: Int, mess: String) {
//
//        try {
//            if (code == BaseContents.Code_200) {
////                if (!TextUtils.isEmpty(mess)) {
////
////
////                    val baseBean = JSON.parseObject<InterBean>(mess, InterBean::class.java)
////                    when (baseBean.ret) {
////                        ApiContents.AGAIN_LOGIN ->
////
////                            mIView?.onAgainLogin()
////                        ApiContents.SUCCESS -> {
////                                when(baseBean.data.code){
////                                    ApiContents.AGAIN_LOGIN ->
////
////                                        mIView?.onAgainLogin()
////
////                                }
////                        }
////                        else -> {
////                            mIView?.onError(baseBean.msg)
////                        }
////                    }
////                    return
////
////                    mIView?.dismissLoading()
////                }
//                //mIView?.dismissLoading()
//            } else {
//                //mIView?.onError(mess)
//                // mIView?.dismissLoading()
//            }
//        } catch (e: Exception) {
//            LogUtils.I("err:$mess")
//        }
//    }

    override fun onComplete() {
        mIView?.dismissLoading()
    }

    var handler: Handler? = Handler(Handler.Callback { msg ->

        if (msg.what == 0) {

        }


        false
    })
}